package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.VO.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.VO.BrandStandardVO;
import cn.tedu.csmall.product.pojo.VO.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.dto.SpuAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.entity.SpuDetail;
import cn.tedu.csmall.product.service.ISpuService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SpuServiceImpl implements ISpuService {

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SpuDetailMapper spuDetailMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AlbumMapper albumMapper;
    @Override
    public void addNew(SpuAddNewDTO addNewDTO) {
        log.debug("开始处理新增spu的业务，接受到的参数是：{}",addNewDTO);

        // 检查相册是否存在
        AlbumStandardVO album = albumMapper.getStandardById(addNewDTO.getAlbumId());
        if (album == null) {
            String message = "添加数据失败，提交的相册数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        // 检查品牌是否存在
        BrandStandardVO brand = brandMapper.getStandardById(addNewDTO.getBrandId());
        if (brand == null) {
            String message = "添加数据失败，提交的相册数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
            // 是否被禁用
        } else if (brand.getEnable() == 0) {
            String message = "添加数据失败，该品牌已经被禁用";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        // 检查类别是否存在，
        CategoryStandardVO category = categoryMapper.getStandardById(addNewDTO.getCategoryId());
        if (category == null) {
            String message = "添加数据失败，提交的类别数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
            // 是否被禁用，
        } else if (category.getEnable() == 0) {
            String message = "添加数据失败，该类别已经被禁用";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            // 是否为父级类别
        } else if (category.getIsParent() == 1) {
            String message = "添加数据失败，该类别存在子级类别，请处理后重试";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        // 创建Spu对象
        Spu spu = new Spu();
        // 调用BeanUtils.copyProperties()复制属性
        BeanUtils.copyProperties(addNewDTO,spu);
        // 补全Spu对象中属性的值：id >>> 暂时写死为某个值
        spu.setId(1L);
        // 补全Spu对象中属性的值：brandName
        spu.setBrandName(brand.getName());
        // 补全Spu对象中属性的值：categoryName
        spu.setCategoryName(category.getName());
        // 补全Spu对象中属性的值：sales, commentCount, positiveCommentCount >>> 0
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        // 补全Spu对象中属性的值：isDelete, isPublished >>> 0
        spu.setIsDeleted(0);
        spu.setIsPublished(0);
        // 补全Spu对象中属性的值：isNewArrival >>> 1
        spu.setIsNewArrival(1);
        // 补全Spu对象中属性的值：isChecked >>> 0, checkUser >>> null, gmtCheck >>> null
        spu.setIsChecked(0);
        spu.setCheckUser(null);
        spu.setGmtCheck(null);
        // 调用SpuMapper对象的insert()执行插入
        int insert = spuMapper.insert(spu);
        if (insert != 1) {
            String message = "当前服务器繁忙，请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        // 创建SpuDetail对象
        SpuDetail spuDetail = new SpuDetail();
        // 补全SpuDetail对象中属性的值：spuId >>> spu.getId()
        spuDetail.setSupId(spu.getId());
        // 补全SpuDetail对象中属性的值：detail >>> 来自参数对象
        spuDetail.setDetail(addNewDTO.getDetail());
        // 调用SpuDetailMapper对象的insert()执行插入
        int i = spuDetailMapper.insert(spuDetail);
        if (i != 1) {
            String message = "当前服务器繁忙，请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }


}
